// pages/ucenter/fenxiaoshang/fenxiaoshang.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');

//获取应用实例
var app = getApp();
var isSend = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sendMsgShow:true,
    mobile: '',
    name: '',
    msgCode: '',
    msgIntervalTime: 60,
    msgIntervalTimeLast: 60,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    isSend = 0;

    var that = this;
    util.request(api.DistributorInfo, {}, 'GET').then(function (data) {
      
      if (data.type === 'success') {
        that.setData({
          applyed: data.data.applyed,
          applyDistributor: data.data.applyDistributor
        });
      }

    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },



  bindinputName:function(e){
    var v = e.detail.value;
    this.setData({
      name: v
    })
  },
  bindinputMobile: function (e) {
    var v = e.detail.value;
    this.setData({
      mobile: v
    })
  },

  bindinputMsgCode: function (e) {
    var v = e.detail.value;
    this.setData({
      msgCode: v
    })
  },

  //获取验证码
  sendMsg: function () {
    if (isSend == 1) {
      return false;
    }
    isSend = 1;
    var that = this;
    var mobile = this.data.mobile;
    if (mobile == null || mobile == '') {
      util.showErrorToast('请输入手机号');
    }
    var data = {
      mobile: mobile
    }
    util.request(api.SendMsgCode, data, 'POST').then(function (data) {
      if (data.type === 'success') {
        util.showSuccessToast(data.content);
        isSend = 0;
        //屏蔽发送按钮+倒计时 
        that.setData({
          sendMsgShow: false
        })
        var msgIntervalTime = that.data.msgIntervalTime;
        var msgIntervalTimeLast = that.data.msgIntervalTimeLast;
        msgIntervalTimeLast = msgIntervalTime;
        that.setData({
          msgIntervalTimeLast: msgIntervalTimeLast
        });
        var interval = setInterval(function () {
          that.setData({
            msgIntervalTimeLast: msgIntervalTimeLast
          })
          msgIntervalTimeLast--;
          if (msgIntervalTimeLast == 0) {
            that.setData({
              sendMsgShow: true
            })
            clearInterval(interval);
          }
        }.bind(this), 1000);
      } else {
        util.showErrorToast(data.content);
        isSend = 0;
      }
    });
  },

  //提交信息
  tijiao: function (e) {
    var name = this.data.name;
    var mobile = this.data.mobile;
    var msgCode = this.data.msgCode;
    var data = {
      name: name,
      mobile: mobile,
      msgCode: msgCode
    }
    wx.showLoading({
      title: '提交中...',
    })
    util.request(api.DistributorSubmit, data, 'POST').then(function (data) {
      wx.hideLoading();
      if (data.type === 'success') {
        wx.showModal({
          title: '提示',
          content: data.content,
          showCancel:false,
          complete:function(){
            wx.navigateBack({ changed: true });//返回上一页
          }
        })
      } else {
        util.showErrorToast(data.content);
      }
    });
  },

  reapply:function(){
    this.setData({
      applyed:0
    })
  }


})